package cn.edu.nju.software.grapheditor.cmd;

import java.awt.Graphics;
import java.awt.Point;
import cn.edu.nju.software.grapheditor.Drawing;
import cn.edu.nju.software.grapheditor.shape.*;

/** @author smy */
public class CopyCmd extends Command {
	private Graphics g;
	private Shape s;
	private int origindex;
	private Shape current;

	public CopyCmd(Graphics g) {
		this.g = g;
	}

	public void executePress(Point p, Drawing dwg) {
		this.s=dwg.getFrontmostContainer(p);
		this.origindex=dwg.GetIndex(s);
		String origtype=s.getclass();
		if(origtype=="Oval"){
			Oval a=(Oval)s;
			this.current=new Oval(a.getColor(),a.getx(),a.gety(),a.getwidth(),a.getheight());
		}
		else if(origtype=="Rectangle"){
			Rectangle a=(Rectangle)s;
			this.current=new Rectangle(a.getColor(),a.getargs()[0],a.getargs()[1],a.getargs()[2],a.getargs()[3]);
		}
		else if(origtype=="Segment"){
			Segment a=(Segment)s;
			this.current=new Segment(a.getColor(),a.getargs()[0],a.getargs()[1],a.getargs()[2],a.getargs()[3]);
		}
		dwg.addshape(this.current,this.origindex+1);
	}
	
	public void executeDrag(Point p,Drawing dwg){
		this.current.setCenter(p);
		dwg.draw(g);
	}
}
